% input: V矩阵, 表示地图上两个点之间的边(a, b, 距离)
% V = [1 2 100;
%      3 4 200;
%      5 2 150;
%      1 3 135];

function G = GraphConvert(tb_originEdges, tb_originNodes)
V = table2array(tb_originEdges);
v_size = size(V);
G = graph(V(:,1).', V(:, 2).', V(:, 3).');
figure(1);
% subplot(1, 2, 1);
% LWidths = 0.05*G.Edges.Weight.^2/max(G.Edges.Weight);
% plot(G, 'EdgeLabel', G.Edges.Weight, 'LineWidth',LWidths);
if(nargin == 2)
    nodeLabel = tb_originNodes.Name;
    plot(G, 'EdgeLabel', G.Edges.Weight, 'NodeLabel', nodeLabel);
else
    plot(G);
end
title('原图');

% 边变成节点
% 相连 没有权重
% new_nodes = 1:v_size(1); % 新节点编号
node1 = V(:, 1);
node2 = V(:, 2);
road_len = V(:, 3); % 节点属性为边长
G = graph;
G = addnode(G, table(node1, node2, road_len)); % 节点存在属性road_len
display(G.Nodes);
for i = 1:v_size(1)
    j = i + 1;
    while j <= v_size(1)
        if any(V(i, 1) == V(j, 1:2)) || any(V(i, 2) == V(j, 1:2))
            G = addedge(G, i, j);
        end
        j = j + 1;
    end
end
% subplot(1, 2, 2);
figure(2);
nodeLabel = string(table2array(G.Nodes));
nodeLabel = "(" + nodeLabel(:, 1) + "," + nodeLabel(:, 2) + "," + nodeLabel(:, 3) + ")"; 
plot(G, 'NodeLabel', nodeLabel);
view(-90, 90);
title('转换后');
end
